package com.abing.starry.util;


import cn.hutool.core.date.DateField;
import cn.hutool.core.date.DateTime;
import cn.hutool.jwt.JWT;
import cn.hutool.jwt.JWTHeader;
import cn.hutool.jwt.JWTPayload;
import cn.hutool.jwt.JWTUtil;
import com.abing.starry.common.ErrorCode;
import com.abing.starry.exception.ThrowUtils;
import com.abing.starry.model.entity.UserEntity;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

import static com.abing.starry.constant.SaltConstant.SALT;

/**
 * @Author CaptainBing
 * @Date 2023/9/21 21:39
 * @Description
 */
public class TokenUtils {

    /**
     * 获取request对象
     * @return
     */
    public static HttpServletRequest getRequest() {
        return ((ServletRequestAttributes) Objects.requireNonNull(RequestContextHolder.getRequestAttributes())).getRequest();
    }

    /**
     * 获取当前用户
     * @return
     */
    public static UserEntity getCurrentUser(){
        String token = getRequest().getHeader("X-Token");
        ThrowUtils.throwIf(StringUtils.isEmpty(token), ErrorCode.NO_AUTH_ERROR);
        JWT jwt = JWTUtil.parseToken(token);
        String id = String.valueOf(jwt.getPayload("id"));
        String userAccount = String.valueOf(jwt.getPayload("userAccount"));
        String userName = String.valueOf(jwt.getPayload("userName"));
        String userRole = String.valueOf(jwt.getPayload("userRole"));
        return UserEntity.builder()
                         .id(id)
                         .userAccount(userAccount)
                         .userName(userName)
                         .userRole(userRole)
                         .build();
    }

    /**
     * 获取当前用户token
     * @return
     */
    public static String getToken(){
        String token = getRequest().getHeader("X-Token");
        ThrowUtils.throwIf(StringUtils.isEmpty(token), ErrorCode.NO_AUTH_ERROR);
        return token;
    }

    public static String genToken(UserEntity userEntity){

        Map<String,Object> jwtHeader = new HashMap<String,Object>(2){{
            put(JWTHeader.TYPE, "Jwt");
            put(JWTHeader.ALGORITHM, "HS256");
        }};

        DateTime now = DateTime.now();
        DateTime expireTime = now.offsetNew(DateField.HOUR, 2);
//        DateTime expireTime = now.offsetNew(DateField.SECOND, 3);
        Map<String,Object> jwtPayload = new HashMap<String,Object>(2){{
            //签发时间
            put(JWTPayload.ISSUED_AT, now);
            //过期时间
            put(JWTPayload.EXPIRES_AT, expireTime);
            //生效时间
            put(JWTPayload.NOT_BEFORE, now);
            put("id", userEntity.getId());
            put("userAccount", userEntity.getUserAccount());
            put("userName", userEntity.getUserName());
            put("userRole", userEntity.getUserRole());
        }};

        return JWTUtil.createToken(jwtHeader,jwtPayload,SALT.getBytes());
    }

}
